let TX_ACCOUNT = "腾讯云账号";
let ACCOUNT_TYPE_ALI = 1;
let ACCOUNT_TYPE_TX = 2;
layui.use(['jquery','element','table','layer','form','util','laydate'], function(){
    var table = layui.table,
        $ = layui.jquery,
        laydate = layui.laydate,
        layer = layui.layer;
    var form=layui.form;
    var util = layui.util;

    let accountType;

    //执行一个laydate实例
    var beginDateInit = function(startId,endId){
        laydate.render({
            elem: startId,
            type: 'datetime',
            done: function (value ) {
                var startDate = new Date(value).getTime();
                var endTime = new Date($(endId).val()).getTime();
                if (endTime < startDate) {
                    layer.msg('结束时间不能小于开始时间');
                    $(startId).val('');
                }
            }
        })
    }
    var endDateInit = function(startId,endId){
        laydate.render({
            elem: endId,
            type: 'datetime',
            done: function (value ) {
                var startDate = new Date($(startId).val()).getTime();
                var endTime = new Date(value).getTime();
                if (endTime < startDate) {
                    layer.msg('结束时间不能小于开始时间');
                    $(endId).val('');
                }
            }
        })
    }


    // 发送时间
    beginDateInit('#beginTime','#endTime');
    endDateInit('#beginTime','#endTime');

    initAkInfo();

    function initAkInfo() {
        $.get('/cdsmsmgr/smsAkInfo/getAkInfoByUser', {}, function (result) {
            if(result != null){
                if(result.resultContent != null){
                    var akInfo = result.resultContent;
                    if(akInfo){
                        accountType = akInfo.thirdAccountType == TX_ACCOUNT ? ACCOUNT_TYPE_TX : ACCOUNT_TYPE_ALI;
                        renderSendRecordTable(accountType);
                    }
                }
            }
        });
    }

    function renderSendRecordTable(accountType){
        table.render({
            elem: '#sendRecordsTable'
            ,url: '/cdsmsmgr/smsSendRecord/list'
            ,method: 'get'
            ,where: {accountType:accountType}
            ,title: '短信发送记录表'
            ,cols: [[
                {type:'checkbox'},
                {type:'numbers',title: '序号'},
                {field: 'id', title: '记录id',hide:true},
                {field:'userName', title:'用户姓名',align : 'center' },
                {field:'phoneNumber', title:'手机号',align : 'center' },
                {field:'sendTime', title:'发送时间',align : 'center', sort: true,templet:function(d){
                        return formatTime(d.sendTime);
                    } }
                ,{field:'operateUser', title:'操作人员', align : 'center' }
                ,{field:'templateId', title:'短信模板CODE', align : 'center' }
                ,{field:'sendContent', title:'发送内容', align : 'center' }
                ,{fixed: 'right', title:'操作', align : 'center',toolbar: '#barSendRecordOperate'}
            ]]
            ,page: true
        });
    }

    // 批量删除
    form.on('submit(batchDelete)',function(obj){
        var checkStatus = table.checkStatus('sendRecordsTable');
        var	data = checkStatus.data;
        if(data.length === 0){
            layer.tips("请至少选择一条记录!",this);
            return false;
        }
        var idArr = [];
        data.forEach(item=>{
            idArr.push(item.id);
        });
        layer.confirm('确定要删除选择的记录么',function(){
            $.ajax({
                url: '/cdsmsmgr/smsSendRecord/batchDelete',
                type: 'post',
                dataType: 'json',
                contentType : "application/json",
                data: JSON.stringify(idArr),
                success: function (res) {
                    layer.msg(res.msg,{
                        time:800,
                        end:function () {
                            table.reload('sendRecordsTable',{page:{curr:1}});
                            layer.closeAll();
                        }
                    });
                },
                error: function () {
                    layer.closeAll();
                    layer.msg("亲,服务器内部错误,请重试或联系管理员!", {icon:5});
                }
            });
        });
        return false;
    });

    function formatTime(time){
        if(time){
            return util.toDateString(time, "yyyy-MM-dd HH:mm:ss");
        }
        return "";
    }
    // 搜索
    form.on('submit(search)',function(data){
        active1['reload'].call(this);
        return false;
    });

    var active1 = {
        reload:function () {
            var userName = $.trim($("#userName").val());
            var sendPhone = $.trim($("#sendPhone").val());
            var startDate= $("#beginTime").val();
            var endDate= $("#endTime").val();
            table.reload('sendRecordsTable', {
                where: {userName:userName,phone:sendPhone,startDate:startDate,endDate:endDate,accountType:accountType}
                ,page: {curr:1}
            }, 'data');
        }
    };

    table.on('tool(sendRecordsTable)', function(obj){
        switch (obj.event) {
            case 'sendRecordDelete':
                sendRecordDelete(obj.data.id);
                break;
        }
    });
    function sendRecordDelete(id) {
        layer.confirm('确定要删除么?',function(){
            var param = {"id":id};
            $.get("/cdsmsmgr/smsSendRecord/deleteById",param, function(data) {
                if (data.resultCode === '000') {
                    layer.msg('删除成功', {icon:1});
                    active1['reload'].call(this);
                    setTimeout(function () {
                        layer.closeAll();
                    },500);
                    return false;
                }
                layer.msg('删除失败',{icon:5});
            });
        });
    }

});
